package com.cql.framework.config.security.service;

import com.cql.common.core.DO.UserDO;
import com.cql.common.utils.StringUtils;
import com.cql.service.business.SysMenuService;
import com.cql.service.business.SysRoleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.Set;

/**
 * @author cql
 */
@Service
public class PermissionService {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());


    @Autowired
    private SysMenuService menuService;

    @Autowired
    private SysRoleService roleService;


    /**
     * 查询用户角色信息
     *
     * @param user
     * @return
     */
    public Set<String> getRolePermission(UserDO user) {
        Set<String> roles = new HashSet<String>();

        if (StringUtils.isNull(user)) {
            throw new RuntimeException("系统繁忙，请稍后再试");
        }

        roles.addAll(roleService.listRolePermissionByUserId(user.getUserId()));

        return roles;
    }

    /**
     * 查询用户所有的权限信息（权限字符串）
     *
     * @param user
     * @return
     */
    public Set<String> getMenuPermission(UserDO user) {
        Set<String> perms = new HashSet<String>();

        if (StringUtils.isNull(user)) {
            throw new RuntimeException("系统繁忙，请稍后再试");
        }

        perms.addAll(menuService.listMenuPermsByUserId(user.getUserId()));

        return perms;
    }


}
